package com.qf.jie.controller;

import com.qf.jie.controller.servlet.BaseServlet;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.AdminService;
import com.qf.jie.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/AdminController")
public class AdminController extends BaseServlet {
    private AdminService adminService = new AdminServiceImpl();

    // 1.管理员登录
    protected RespBean login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        return adminService.login(req, name, password);
    }

    // 2.分页获取用户信息
    protected RespBean getUserPageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String len = req.getParameter("len");
        return adminService.getPage(currentPage, len);
    }
    // 3.退出登录
    protected RespBean logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("admin",null);
        return RespBean.respSuccess("退出成功");
    }
    // 4.删除用户
    protected RespBean deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        return adminService.deleteUser(id);
    }
    // 5.修改用户
    protected RespBean updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String user_type = req.getParameter("user_type");
        String money = req.getParameter("money");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        return adminService.updateUser(id, username, password, user_type, money, sex, phone);
    }
    // 6.查看所有商品
    protected RespBean getCommodityPageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String len = req.getParameter("len");
        return adminService.getCommodityPageList(currentPage, len);
    }
    // 7.删除商品
    protected RespBean deleteCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        return adminService.deleteCommodity(id);
    }


}
